package com.library.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.common.Result;
import com.library.entity.User;
import com.library.service.IUserService;
import com.library.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  用户 控制层
 * </p>
 *
 * @author LDH
 * @since 2022-05-12
 */
@RestController
@RequestMapping("/library/users")
@Api("用户接口")
public class UserController {

    @Autowired
    private IUserService userService;


    @ApiOperation(value = "用户登录",notes = "<span style='color:red;'>详细描述：</span>&nbsp;根据用户账号密码登录")
    @PostMapping("/login")
    public Result userLogin(@Valid @RequestParam String account, @RequestParam String password){
        return userService.userLogin(account,password);
    }

    @ApiOperation(value = "用户注册",notes = "<span style='color:red;'>详细描述：</span>&nbsp;传一个用户信息过来，注册用户,要注意有些信息可以空有些<span style='color:red;'>不能为空！</span>")
    @PostMapping(value = "/register",produces = "application/json;charset=UTF-8")
    public Result saveUser(@Valid @RequestBody User user){
        return userService.saveUser(user);
    }

    @ApiOperation(value = "获取所有用户信息",notes = "<span style='color:red;'>详细描述：</span>&nbsp;获取所有用户信息")
    @GetMapping
    public Result saveAll(){
        return userService.saveAll();
    }

    @ApiOperation(value = "根据id更新用户",notes = "<span style='color:red;'>详细描述：</span>&nbsp;根据id更新用户")
    @PutMapping(produces = "application/json;charset=UTF-8")
    public Result updateUser(@Valid @RequestBody User user){
        return userService.updateUser(user);
    }

    @ApiOperation(value = "根据id删除用户",notes = "<span style='color:red;'>详细描述：</span>&nbsp;根据id，指谁删谁")
    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable Integer id){
        return userService.deleteUser(id);
    }

    @ApiOperation(value = "根据id批量删除用户",notes = "<span style='color:red;'>详细描述：</span>&nbsp;传一组id数组过来,全咔咔掉！")
    @DeleteMapping("/users")
    public Result deleteByIdList(@Valid @RequestBody Map<String,List> ids){
        return userService.deleteByIdList(ids);
    }

    @ApiOperation(value = "根据id批量编辑用户",notes = "<span style='color:red;'>详细描述：</span>&nbsp;传一组用户数组过来，全改掉！")
    @PutMapping("/users")
    public Result updateByUserList(@Valid @RequestBody Map<String,List<User>> users){
        return userService.updateByUserList(users);
    }

    @ApiOperation(value = "模糊查询用户",notes = "<span style='color:red;'>详细描述：</span>&nbsp;可以根据用户的姓名和昵称模糊查询")
    @GetMapping("/name")
    public Result likeByName(@RequestParam String name){
        return userService.likeByName(name);
    }

    @ApiOperation(value = "用户分页查询",notes = "<span style='color:red;'>详细描述：</span>&nbsp;要第几页就传个数字过来，在路径后面加")
    @GetMapping("/page/{p}")
    public Result page(@PathVariable Integer p){
        return userService.findPage(p);
    }

}
